package com.jyu.apts.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.QRCodeDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.ItemProcessRecord;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.QRCode;
import com.jyu.apts.entity.QRCodeTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.QRCodeStatusEnum;
import com.jyu.apts.entityEnum.QRCodeTypeEnum;
import com.jyu.apts.service.ItemProcessRecordService;
import com.jyu.apts.service.QRCodeService;
import com.jyu.apts.service.QRCodeTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class OrganizationNormalQRCodeController {

	@Autowired
	QRCodeService qrCodeService;
	@Autowired
	QRCodeTemplateService qrCodeTemplateService;
	@Autowired
	UserService userService;
	@Autowired
	ItemProcessRecordService itemProcessRecordService;
	
	@RequiresPermissions("organizationNormalQRCode_html")
	@RequestMapping("/organizationNormalQRCode_html")
	public ModelAndView organizationNormalQRCode_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationNormalQRCode.html");
		return mv;
	}
	
	@RequiresPermissions("organizationNormalQRCode_html")
	@RequestMapping("/addOrganizationNormalQRCode")
	public ResultDto addOrganizationNormalQRCode(String[] dataValues,long qrCodeTemplateId,String itemProcessRecordId) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		QRCodeTemplate organizationNormalQRCodeTemplate = qrCodeTemplateService.getOne(qrCodeTemplateId);
		if(organizationNormalQRCodeTemplate == null) {
			return new ResultDto("二维码模板不存在",null,2);
		}
		QRCode organizationNormalQRCode = new QRCode();
		organizationNormalQRCode.setAuthor(user);
		organizationNormalQRCode.setCode(UUID.randomUUID().toString().replaceAll("-", ""));
		if(itemProcessRecordId!=null && itemProcessRecordId.trim().length()!=0) {
			long itemProcessRecordIdL = Long.parseLong(itemProcessRecordId);
			ItemProcessRecord itemProcessRecord = itemProcessRecordService.getOne(itemProcessRecordIdL);
			if(itemProcessRecord == null) {
				return new ResultDto("输入的流程记录不存在",null,2);
			}
			else if(itemProcessRecord.getItemBatchRecord().getItemProductionLine().getItem().getOrganization().getId()!=organization.getId()) {
				return new ResultDto("非本机构流程记录",null,2);
			}
			else {
				organizationNormalQRCode.setItemProcessRecord(itemProcessRecord);
			}
		}
		StringBuilder sb = new StringBuilder("");
		if(dataValues == null || dataValues.length == 0) {
			sb.append("#");
		}
		for(String dataValue : dataValues) {
			sb.append("#");
			sb.append(dataValue);
		}
		organizationNormalQRCode.setDataValues(sb.toString().substring(1));
		organizationNormalQRCode.setType(QRCodeTypeEnum.NORMAL.getType());
		organizationNormalQRCode.setQrCodeTemplate(organizationNormalQRCodeTemplate);
		organizationNormalQRCode.setStatus(QRCodeStatusEnum.UNUSED.getType());
		qrCodeService.save(organizationNormalQRCode);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organizationNormalQRCode_html")
	@RequestMapping("/delOrganizationNormalQRCode")
	public ResultDto delQRCode(String ids) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Organization organization = user.getOrganization();
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : ids.split(",")) {
			l_ids.add(new Long(id));
		}
		List<QRCode> organizationNormalQRCodes = qrCodeService.findAllById(l_ids);
		for(QRCode qrCode : organizationNormalQRCodes) {
			if(qrCode.getType()!=QRCodeTypeEnum.NORMAL.getType())
				return new ResultDto("存在非普通二维码",null,2);
			if(qrCode.getStatus()!=QRCodeStatusEnum.UNUSED.getType())
				return new ResultDto("存在使用中的二维码",null,2);
			if(qrCode.getAuthor().getOrganization().getId()!=organization.getId())
				return new ResultDto("存在非本机构二维码",null,2);
		}
		qrCodeService.delete(organizationNormalQRCodes);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organizationNormalQRCode_html")
	@RequestMapping("/getOrganizationNormalQRCodePage")
	public TableDto<QRCodeDto> getQRCodePage(Integer pageNumber,Integer pageSize,Integer status,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		Long organizationId = user.getOrganization().getId();
		Page<QRCode> pageResult = qrCodeService.getQRCodePage(pageNumber, pageSize,QRCodeTypeEnum.NORMAL.getType(),status, organizationId, search);
		List<QRCode> organizationNormalQRCodes = pageResult.getContent();
		List<QRCodeDto> organizationNormalQRCodeDtos = new ArrayList<QRCodeDto>();
		organizationNormalQRCodes.forEach(organizationNormalQRCode->organizationNormalQRCodeDtos.add(new QRCodeDto().convert(organizationNormalQRCode)));
		return new TableDto<QRCodeDto>(organizationNormalQRCodeDtos,pageResult.getTotalElements());
	}
	
}
